package com.yuncheng.sms.utils;

import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 短信验证辅助功能
 *
 * @date 2023/4/10
 */
public class SmsHelper {

    //内网ip的正则表达式
    public static final Pattern INNER_NETWORK_IP_PATTERN = Pattern.compile("^(127\\.0\\.0\\.1)|(localhost)|(10\\.0\\.\\d{1,3}\\.\\d{1,3})|(192\\.168\\.\\d{1,3}\\.\\d{1,3})|(172\\.(16|26|45|145|18)\\.\\d{1,3}\\.\\d{1,3})$");

    public static boolean isInnerNetworkIp(String ipv4) {
        if (StringUtils.hasText(ipv4)) {
            Matcher matcher = INNER_NETWORK_IP_PATTERN.matcher(ipv4.trim());
            return matcher.matches();
        }
        return false;
    }

    public static void main(String[] args) {
        String[] ipinner = {
                "127.0.0.1",
                "localhost",
                "10.0.100.200",
                "10.1.100.200",
                "192.168.0.100",
                "192.168.100.200",
                "172.16.100.10",
                "172.26.200.30",
                "172.45.40.50",
                "172.145.60.78",
                "172.18.90.11"
        };
        String[] ipout = {
                "a.com",
                "10.1.100.200",
                "192.169.0.100",
                "193.168.100.200",
                "172.0.100.10",
                "172.1.100.10",
                "172.10.100.10",
                "172.15.100.10",
                "172.16.100.10",
                "172.20.100.10",
                "172.21.100.10",
                "172.25.100.10",
                "172.32.100.10",
                "172.42.100.10",
                "172.130.100.10",
                "172.144.60.78"
        };

        int errcount = 0;
        for (int i = 0; i < ipinner.length; i++) {
            if (!isInnerNetworkIp(ipinner[i])) {
                errcount++;
                System.out.println("ipinner not inner:" + ipinner[i]);
            }
        }
        System.out.println("ipinner check:" + ipinner.length + ", errcount=" + errcount);

        errcount = 0;
        for (int i = 0; i < ipout.length; i++) {
            if (isInnerNetworkIp(ipout[i])) {
                errcount++;
                System.out.println("ipout inner:" + ipout[i]);
            }
        }
        System.out.println("ipout check:" + ipinner.length + ", errcount=" + errcount);

    }
}
